#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define repd(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define forn(i,a,b) for(int i=(int)(a);i<=(int)(b);++i)
#define ford(i,a,b) for(int i=(int)(b);i>=(int)(a);--i)
#define all(S) (S).begin(),(S).end()
#define pb push_back
#define INF 0x3f3f3f3f
#define modcg(x) if(x>=mod)x-=mod
#define modcl(x) if(x<0)x+=mod
//============================================================================================
const int MAXN=1e5+10,MAXM=22;
int n,m,k,T;
int a1[MAXN],c[MAXN],A[4],a[MAXN];
string s[MAXN];
int glog(int x)
{
    x--;
    int ans=0;
    while(x)
    {
        ans++;x>>=1;
    }
    return ans;
}
namespace SUB2
{
    int dp[MAXN<<3];
    void push_up(int now,int id,int dep)
    {
        if((dp[now<<1|1]==-1)||(dp[now<<1]==-1))
        {
            // cout<<1<<" "<<now<<" "<<id<<" "<<dep<<endl;
            dp[now]=-1;
        }
        else
        {
            if(a[dp[(now<<1)|(s[dep][id]-'0')]]>=dep)
            {
                // cout<<2<<" "<<now<<" "<<id<<" "<<dep<<endl;
                dp[now]=dp[(now<<1)|(s[dep][id]-'0')];
            }
            else
            {
                // cout<<3<<" "<<now<<" "<<id<<" "<<dep<<endl;
                // cout<<((now<<1)|((s[dep][id]-'0')^1))<<endl;
                dp[now]=dp[(now<<1)|((s[dep][id]-'0')^1)];
            }
        }
    }
    void build(int now,int l,int r,int id,int dep)
    {
        if(l==r)
        {
            if(l<=n)
            {
                dp[now]=l;
            }
            else
            {
                dp[now]=-1;
            }
            return;
        }
        int mid=(l+r)>>1;
        build(now<<1,l,mid,id<<1,dep-1);
        build(now<<1|1,mid+1,r,id<<1|1,dep-1);
        push_up(now,id,dep);
        // cout<<now<<" "<<l<<" "<<r<<" "<<id<<" "<<dep<<" "<<dp[now]<<endl;
    }
    pii pc(pii x,pii y,int op,int r)
    {
        if(!op)
        {
            if(!x.second)
            {
                if(a[x.first]>=r)
                {
                    return {x.first,0};
                }
                else
                {
                    return y;
                }
            }
            else
            {
                if(a[x.first]>=r)
                {
                    return {x.first,x.second+y.second};
                }
                else
                {
                    return {0,x.second+y.second};
                }
            }
        }
        else
        {
            if(!x.second)
            {
                if(y.first&&a[y.first]>=r)
                {
                    return {y.first,y.second};
                }
                else
                {
                    return {x.first,y.second};
                }
            }
            else
            {
                return {x.first,y.second+x.second};
            }
        }
    }
    pii qurey(int now,int l,int r,int qr,int id,int dep)
    {
        if(r<=qr)
        {
            return {dp[now],0};
        }
        if(l>qr)
        {
            return {0,(l+r)*(r-l+1)/2};
        }
        int mid=(l+r)>>1;
        pii x=qurey(now<<1,l,mid,qr,id<<1,dep-1),y=qurey(now<<1|1,mid+1,r,qr,id<<1|1,dep-1);
        pii ans=pc(x,y,s[dep][id]-'0',dep);
        // cout<<now<<" "<<l<<" "<<r<<" "<<qr<<" "<<id<<" "<<dep<<" "<<ans.first<<" "<<ans.second<<endl;
        return ans;
    }
    int calc()
    {
        // forn(i,1,n)
        // {
        //     cout<<a[i]<<" ";
        // }
        // cout<<endl;
        build(1,1,1<<k,0,k);
        int ans=0;
        forn(i,1,m)
        {
            int ck=glog(c[i]);
            pii now=qurey(1<<(k-ck),1,1<<ck,c[i],0,ck);
            int x=now.first+now.second;
            // cout<<c[i]<<" "<<now.first<<" "<<now.second<<endl;
            ans^=(i*x);
        }
        return ans;
    }
    void solve()
    {
        forn(_,1,T)
        {
            rep(i,4)
            {
                cin>>A[i];
            }
            forn(i,1,n)
            {
                a[i]=a1[i]^A[i%4];
            }
            cout<<calc()<<endl;
        }
    }
};
void SOLVE()
{
    cin>>n>>m;
    forn(i,1,n)
    {
        cin>>a1[i];
    }
    forn(i,1,m)
    {
        cin>>c[i];
    }
    k=glog(n);
    forn(i,1,k)
    {
        cin>>s[i];
    }
    cin>>T;
    SUB2::solve();
}
signed main()
{
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin.tie(0);cout.tie(0);std::ios::sync_with_stdio(0);
    int TEST_CASE=1;
    // cin>>TEST_CASE;
    while(TEST_CASE--)
    {
        SOLVE();
    }
    return 0;
}